var randomUtils = require ('./randomUtils');
var clickUtils = require ('./clickUtils');
var logUtils = require ('./logUtils');
var pointMap = require ('../pointMap/index');
module.exports = {
  openApps (apps) {
    for (var index = 0; index < apps.length; index++) {
      var app = apps[index];
      this.open (app);
      sleep (3.5 * 1000);
    }
  },
  open (appName) {
    this.appName = appName;
    var result = app.launchApp (appName);
    if (!result) {
      logUtils.info ('打开：', appName, '失败');
    } else {
      logUtils.info ('打开：', appName, '成功');
    }
    return result;
  },
  closeRecent () {
    home ();
    sleep (randomUtils.getRandom (2.5, 3, 1000));
    recents ();
    sleep (randomUtils.getRandom (2.5, 3, 1000));
    var deviceProduct = device.product;
    if (deviceProduct == 'cannon' || deviceProduct == 'evergo') {
      //note11
      logUtils.info (deviceProduct, '开始关闭应用');
      swipe (
        device.width / 4 + device.width / 8 + device.width / 16,
        device.height / 2,
        device.width / 20,
        device.height / 2,
        200
      );
    } else {
      //12C
      logUtils.info (deviceProduct, '开始滑动');
      swipe (
        device.width / 2,
        device.height / 2,
        device.width / 2,
        device.height / 16,
        randomUtils.getRandom (300, 500)
      );
    }
    sleep (randomUtils.getRandom (1, 1.5, 1000));
    home ();
    sleep (randomUtils.getRandom (2.5, 3, 1000));
    logUtils.info ('关闭最近应用:', this.appName || '', '成功');
    return true;
  },
  closeAll () {
    home ();
    sleep (randomUtils.getRandom (2.5, 3, 1000));
    recents ();
    sleep (randomUtils.getRandom (2.5, 3, 1000));
    clickUtils.clickPointMap (pointMap.app.关闭所有应用);
  },
  checkInstalled (appName) {
    return !!app.getPackageName (appName);
  },
};
